package com.zpark.wxshop.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpark.wxshop.Vo.VipcardVo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zpark.wxshop.Utils.Result;

import com.zpark.wxshop.service.IVipcardService;
import com.zpark.wxshop.entity.Vipcard;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author PJone
 * @since 2023-09-10
 */
@RestController
@RequestMapping("/vipcard")
public class VipcardController {

    @Resource
    private IVipcardService vipcardService;

    /**
    * 新增与修改
    */
    @PostMapping
    public Result save(@RequestBody Vipcard vipcard) {
        vipcardService.saveOrUpdate(vipcard);
        return Result.success();
    }

    /**
    * 根据id删除
    */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        vipcardService.removeById(id);
        return Result.success();
    }

    /**
    * 批量删除
    */
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        vipcardService.removeByIds(ids);
        return Result.success();
    }

    /**
    * 查询所有会员卡
    */
    @GetMapping("/lapse")
    public Result findAll(@RequestParam("uid") Integer uid, @RequestParam("lapse") Integer lapse) {
        return Result.success(vipcardService.findBylapse(uid, lapse));
    }


    /**
    * 根据id查询单个
    */
    @GetMapping("/{cardId}")
    public Result findOne(@PathVariable Integer cardId) {
        return Result.success(vipcardService.findOneByCardId(cardId));
    }

    /**
    * 分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<Vipcard> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(vipcardService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    /**
     * 根据用户id查询用户可用的会员卡
     */
    @GetMapping("/vipcard")
    public Result findByUid(@RequestParam("uid") Integer uid, @RequestParam("sid") Integer sid){
        return Result.success(vipcardService.list(new QueryWrapper<Vipcard>().eq("uid", uid).eq("sid", sid)));
    }

}

